/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.io.Serializable;

public class Abilities implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int STR, DEX, CON, INT, WIS, CHA;
	int STRMOD, DEXMOD, CONMOD, INTMOD, WISMOD, CHAMOD = 0;
	int[] modArray = new int[6];

	public Abilities() {
		STR = -99;
		DEX = -99;
		CON = -99;
		INT = -99;
		WIS = -99;
		CHA = -99;
	}

	public void initializeAbilities(int[] rawAbilityScores) {
		STR = rawAbilityScores[0];
		DEX = rawAbilityScores[1];
		CON = rawAbilityScores[2];
		INT = rawAbilityScores[3];
		WIS = rawAbilityScores[4];
		CHA = rawAbilityScores[5];

	}

	// checks if the requierd fields in Attributes are filled out
	public int checkAbilities() {
		if (STR != -99 && DEX != -99 && CON != -99 && INT != -99 && WIS != -99 && CHA != -99)
			return 1;
		else
			return 0;
	}

	/**
	 * Alters the ability passed it by 'amt' amount
	 * 
	 * @param ability
	 * @param amt
	 */
	public void alterAbility(int ability, int amt) {
		modArray[ability] += amt;
	}

	// Sets all the attributes at once.
	public void setAbilities(int str, int dex, int con, int intel, int wis, int cha) {
		STR = str;
		DEX = dex;
		CON = con;
		INT = intel;
		WIS = wis;
		CHA = cha;

	}

	/**
	 * Sets all the abilities and modifiers accordingly, taking into account the
	 * racial modifiers
	 * 
	 * @param race
	 * @param rawAbilityScores
	 */
	public void initializeCharacterAbilities(String race, int[] rawAbilityScores) {
		STR = rawAbilityScores[0];
		DEX = rawAbilityScores[1];
		CON = rawAbilityScores[2];
		INT = rawAbilityScores[3];
		WIS = rawAbilityScores[4];
		CHA = rawAbilityScores[5];

		applyRaceMods(race);

		setCharacterModifiers();
	}

	/**
	 * Sets the modifiers to the initial value of the Abilities given. This
	 * should be the first method called after the character's abilities are
	 * set.
	 * 
	 * @return
	 */
	public void updateSpecificModifier(int modifier) {
		if (modifier < 10)
			//For numbers < 10
			modifier = (int) Math.ceil((modArray[modifier] - 11) / 2);
		else
			//For numbers >= 10
			modifier = (int) Math.floor((modArray[modifier] - 10) / 2);
	}

	public void setCharacterModifiers() {
		modArray[0] = STR;
		modArray[1] = DEX;
		modArray[2] = CON;
		modArray[3] = INT;
		modArray[4] = WIS;
		modArray[5] = CHA;
		for (int i = 0; i < modArray.length; i++) {
			if (modArray[i] < 10)
				//For numbers < 10
				modArray[i] = (int) Math.ceil((modArray[i] - 11) / 2);
			else
				//For numbers >= 10
				modArray[i] = (int) Math.floor((modArray[i] - 10) / 2);
		}
		STRMOD = modArray[0];
		DEXMOD = modArray[1];
		CONMOD = modArray[2];
		INTMOD = modArray[3];
		WISMOD = modArray[4];
		CHAMOD = modArray[5];
	}

	public int[] getAbilityScoreArray() {
		int[] abilityArray = new int[6];
		abilityArray[0] = STR;
		abilityArray[1] = DEX;
		abilityArray[2] = CON;
		abilityArray[3] = INT;
		abilityArray[4] = WIS;
		abilityArray[5] = CHA;

		return abilityArray;
	}

	/**
	 * Adjusts the character's mods based off their race
	 * 
	 * @param race
	 * @return
	 */
	public void applyRaceMods(String race) {

		if (race.equals(ConstantHelper.RACE_DWARF)) {
			CON += 2;
			CHA -= 2;
		} else if (race.equals(ConstantHelper.RACE_ELF)) {
			DEX += 2;
			CON -= 2;
		} else if (race.equals(ConstantHelper.RACE_GNOME)) {
			CON += 2;
			STR -= 2;
		} else if (race.equals(ConstantHelper.RACE_HALF_ELF)) {
			//No modifiers
		} else if (race.equals(ConstantHelper.RACE_HALF_ORC)) {
			STR += 2;
			INT -= 2;
			CHA -= 2;
		} else if (race.equals(ConstantHelper.RACE_HALFLING)) {
			DEX += 2;
			STR -= 2;
		} else
			;
	}

	public String getModChoiceString(String race) {

		String retStr;
		if (race == ConstantHelper.RACE_DWARF) {
			retStr = "Constitution + 2, Charisma -2";
		} else if (race == ConstantHelper.RACE_ELF) {
			retStr = "Dexterity + 2, Constitution -2";
		} else if (race == ConstantHelper.RACE_GNOME) {
			retStr = "Constitution + 2, Strength -2";
		} else if (race == ConstantHelper.RACE_HALF_ELF) {
			retStr = "No Modifiers for this race";
		} else if (race == ConstantHelper.RACE_HALF_ORC) {
			retStr = "Strength + 2, Charisma -2, Intelegence -2";
		} else if (race == ConstantHelper.RACE_HALFLING) {
			retStr = "Dexterity+ 2, Strength-2";
		} else if (race == ConstantHelper.RACE_HUMAN) {
			retStr = "No Modifiers for this race";
		} else
			retStr = "";
		return retStr;
	}

	/**
	 * @return the sTR
	 */
	public int getSTR() {
		return STR;
	}

	/**
	 * @param sTR
	 *            the sTR to set
	 */
	public void setSTR(int sTR) {
		STR = sTR;
	}

	/**
	 * @return the dEX
	 */
	public int getDEX() {
		return DEX;
	}

	/**
	 * @param dEX
	 *            the dEX to set
	 */
	public void setDEX(int dEX) {
		DEX = dEX;
	}

	/**
	 * @return the cON
	 */
	public int getCON() {
		return CON;
	}

	/**
	 * @param cON
	 *            the cON to set
	 */
	public void setCON(int cON) {
		CON = cON;
	}

	/**
	 * @return the iNT
	 */
	public int getINT() {
		return INT;
	}

	/**
	 * @param iNT
	 *            the iNT to set
	 */
	public void setINT(int iNT) {
		INT = iNT;
	}

	/**
	 * @return the wIS
	 */
	public int getWIS() {
		return WIS;
	}

	/**
	 * @param wIS
	 *            the wIS to set
	 */
	public void setWIS(int wIS) {
		WIS = wIS;
	}

	/**
	 * @return the cHA
	 */
	public int getCHA() {
		return CHA;
	}

	/**
	 * @param cHA
	 *            the cHA to set
	 */
	public void setCHA(int cHA) {
		CHA = cHA;
	}

	/**
	 * @return the sTRMOD
	 */
	public int getSTRMOD() {
		return STRMOD;
	}

	/**
	 * @param sTRMOD
	 *            the sTRMOD to set
	 */
	public void setSTRMOD(int sTRMOD) {
		STRMOD = sTRMOD;
	}

	/**
	 * @return the dEXMOD
	 */
	public int getDEXMOD() {
		return DEXMOD;
	}

	/**
	 * @param dEXMOD
	 *            the dEXMOD to set
	 */
	public void setDEXMOD(int dEXMOD) {
		DEXMOD = dEXMOD;
	}

	/**
	 * @return the cONMOD
	 */
	public int getCONMOD() {
		return CONMOD;
	}

	/**
	 * @param cONMOD
	 *            the cONMOD to set
	 */
	public void setCONMOD(int cONMOD) {
		CONMOD = cONMOD;
	}

	/**
	 * @return the iNTMOD
	 */
	public int getINTMOD() {
		return INTMOD;
	}

	/**
	 * @param iNTMOD
	 *            the iNTMOD to set
	 */
	public void setINTMOD(int iNTMOD) {
		INTMOD = iNTMOD;
	}

	/**
	 * @return the wISMOD
	 */
	public int getWISMOD() {
		return WISMOD;
	}

	/**
	 * @param wISMOD
	 *            the wISMOD to set
	 */
	public void setWISMOD(int wISMOD) {
		WISMOD = wISMOD;
	}

	/**
	 * @return the cHAMOD
	 */
	public int getCHAMOD() {
		return CHAMOD;
	}

	/**
	 * @param cHAMOD
	 *            the cHAMOD to set
	 */
	public void setCHAMOD(int cHAMOD) {
		CHAMOD = cHAMOD;
	}

	/**
	 * @return the modArray
	 */
	public int[] getModArray() {
		return modArray;
	}

	/**
	 * @param modArray
	 *            the modArray to set
	 */
	public void setModArray(int[] modArray) {
		this.modArray = modArray;
	}
}
